#pragma once
#include <iostream>
#include <string>
#include <jsoncpp/json/json.h>

using namespace std;

// Serialize 序列化
// 协议: 报文=报头+有效载荷
// "有效载荷长度"\r\n"有效载荷"\r\n
// "len\r\n"_x _oper _y"\r\n    len: 有效载荷长度, 约定\r\n是分隔符

namespace protocol_ns
{
    const string SEP = "\r\n";
    // TCP有粘包问题
    string Encode(const string &json_str)
    {
        int json_str_len = json_str.size();
        string proto_str = to_string(json_str_len);
        proto_str += SEP;
        proto_str += json_str;
        proto_str += SEP;
        return proto_str;
    }
    // "len"\r\n{    }"\r\n;
    string Decode(string &inbuffer)
    {
        auto pos = inbuffer.find(SEP);
        if (pos == string::npos)
            return string();
        string len_str = inbuffer.substr(0, pos);
        if (len_str.empty())
            return string();
        int packlen = stoi(len_str);

        int total = packlen + len_str.size() + 2 * SEP.size();
        if (inbuffer.size() < total)
            return string();

        string package = inbuffer.substr(pos + SEP.size(), packlen);
        inbuffer.erase(0, total);
        return package;
    }

    class Request
    {
    public:
        Request()
        {
        }
        Request(int x, int y, char oper) : _x(x), _y(y), _oper(oper)
        {
        }

        bool Serialize(string *out)
        {
            Json::Value root;
            root["x"] = _x;
            root["y"] = _y;
            root["oper"] = _oper;

            Json::FastWriter writer;
            // Json::StyledWriter writer;
            *out = writer.write(root);
            return true;
        }

        bool Deserialize(const string &in)
        {
            Json::Value root;
            Json::Reader reader;
            bool res = reader.parse(in, root);
            if (!res)
                return false;

            _x = root["x"].asInt();
            _y = root["y"].asInt();
            _oper = root["oper"].asInt();
            return true;
        }

        ~Request()
        {
        }

    public:
        int _x;
        int _y;
        char _oper;
    };

    class Response
    {
    public:
        Response()
        {
        }
        Response(int result, int code) : _result(result), _code(code)
        {
        }

        bool Serialize(string *out)
        {
            Json::Value root;
            root["result"] = _result;
            root["code"] = _code;

            Json::FastWriter writer;
            // Json::StyledWriter writer;
            *out = writer.write(root);
            return true;
        }

        bool Deserialize(const string &in)
        {
            Json::Value root;
            Json::Reader reader;
            bool res = reader.parse(in, root);
            if (!res)
                return false;

            _result = root["result"].asInt();
            _code = root["code"].asInt();
            return true;
        }

        ~Response()
        {
        }

    public:
        int _result;
        int _code;
    };

    class Factory
    {
    public:
        Factory()
        {
            srand(time(nullptr) ^ getpid());
            opers = "+/*-%|&";
        }
        ~Factory()
        {
        }

        shared_ptr<Request> BuildRequest()
        {
            int x = rand() % 10 + 1;
            usleep(x * 10);
            int y = rand() % 5 + 1;
            usleep(y * x * 5);
            char oper = opers[rand() % opers.size()];

            shared_ptr<Request> req = make_shared<Request>(x, y, oper);
            return req;
        }
        shared_ptr<Response> BuildResponse()
        {
            return make_shared<Response>();
        }

    private:
        string opers;
    };
}